Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
| vs < for stdin and stdout with splice, pipe and tee command in C
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <unistd.h>
#include <fcntl.h> 
#include <stdio.h>
#include <sys/syscall.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
int main(int argc, char ** argv) {
    int fd[2];
    int len,slen,len_out,slen_out;
    pipe(fd);
  
    int f=open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);

    len=tee(STDIN_FILENO,fd[1],INT_MAX,SPLICE_F_MOVE);
    
    while (len > 0) {
        slen = splice(fd[0], NULL, f, NULL, len, SPLICE_F_MOVE);
        len -= slen;
        if(slen==0)
            break;
    }   

    len_out = tee(STDIN_FILENO,fd[1],INT_MAX,SPLICE_F_MOVE);    

    while (len_out > 0) {
        slen_out = splice(fd[0], NULL, STDOUT_FILENO, NULL, len_out, SPLICE_F_MOVE);
            len_out -= slen_out;
            if(slen_out==0)
                break;
    }

    close(fd[1]);
    close(fd[0]);
    close(f);  
    
    return 0;
}

When I run my program with a pipe ex: cat test.txt| ./a.out result.txt The content from test.txt goes into result.txt

But with the other method, ex: ./a.out result.txt <test.txt nothing happens. Isn't it suppose to act the same ?

Also, when I splice to the STDOUT_FILENO, it never print on the standard output, and I do not understand why.

Thank you for your help

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The two methods are not equivalent:

  • cat in.txt | will actually create a pipe to feed your program
  • < in.txt will just open the given file and serve it a fd 0

On second case, tee returns -1, and perror complains about an invalid argument.

You can check the nature of fd 0 using fstat and checking the st_mode value:

    struct stat statbuf;
    fstat(STDIN_FILENO, &statbuf);
    
    if (statbuf.st_mode & S_IFREG) 
        printf("regular file");
    
    if (statbuf.st_mode & S_IFIFO) 
        printf("pipe");

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda